package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;

public class ChoiceQuestion extends Question {
	protected String[] options = null;

	public String[] getOptions(){
		return options;
	}

	public void setOptions(String[] options){
		this.options = options;
	}
	
	public ArrayList<Integer> getRandomOptionKeys(){
		ArrayList<Integer> keys = new ArrayList<Integer>();
		for(int i = 0; i < options.length; i++){
			keys.add(i);
		}
		Collections.shuffle(keys);
		return keys;
	}
	
	public String getOptionString(){
		StringBuffer str = new StringBuffer();
		for(int i = 0; i < options.length; i++){
			str.append('|');
			str.append(options[i]);
		}
		
		return str.substring(1);
	}
	
	public void setOptions(String options){
		this.options = options.split("\\|");
	}
	
	public boolean isMultipleChoice(){
		return getAnswer().length() > 1;
	}
	
	public boolean isSingleChoice(){
		return getAnswer().length() == 1;
	}
	
	static public ChoiceQuestion fromDatabase(ResultSet rs) throws SQLException{
		ChoiceQuestion q = new ChoiceQuestion();
		q.setQid(rs.getInt(1));
		q.setContent(rs.getString(2));
		q.setAnswer(rs.getString(3));
		q.setOptions(rs.getString(4));
		q.setQtid(rs.getInt(5));
		return q;
	}
}
